package main

import (
	"fmt"
	"flag"
	"parser"
	"search_backend"
	"server"
	"log"
	"time"
)

// Flags
var workingDirectory *string = flag.String("docs", "./docs", "Localización de los documentos de texto")
var h *bool = flag.Bool("h", false, "Mostrar la ayuda")
var s *bool = flag.Bool("s", false, "Ejecuta el servidor web")
var i *bool = flag.Bool("i", false, "Ejecuta el indexado")
var t *bool = flag.Bool("t", false, "Muestra la tabla de indice")

// Main function
func main(){

	flag.Parse()

	if *h == true {
		showHelp()
	} else {
		if *s == true {
			server.StartServer(*t)
		} else if *i == true {			
			log.Print("Indexing documents... ")
			tim := time.Seconds()
			parser.ParseFiles(*workingDirectory)
			tim = time.Seconds() - tim
			m := int64(tim/60)
			s := tim - m*60
			log.Printf("Indexing process finished in %d minutes %d seconds", m, s)
			if *t == true {
				search_backend.PrintWordsMap()
			}
			log.Printf("Saving documents index to \"%s\"... ", search_backend.INDEX)
			search_backend.SaveWordsMap()
		} else {
			fmt.Println("Execute the program with a '-s' or '-i' option")
		}
	}
}

// Prints usage help
func showHelp() {
	fmt.Println("Usage: main [OPTIONS]")
	flag.PrintDefaults()
}

